package com.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.pojo.TbOrder;
import com.test.pojo.TbOrderItem;
import com.test.service.TbOrderItemService;
import com.test.service.TbOrderService;
import com.test.vo.OrderShowVO;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanglijun
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/tb-order")
public class TbOrderController {
    @Resource
    TbOrderService orderService;
    @Resource
    TbOrderItemService orderItemService;
    List<TbOrder> orderList=null;
    List<TbOrderItem> itemList=null;
    @RequestMapping("/delOrders")
    public boolean delOrders(String delId){

        TbOrder  delOrder= orderService.getById(delId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",delId);
        delOrder.setIsDelete("1");
        return   orderService.saveOrUpdate(delOrder,queryWrapper);

    }

    @RequestMapping("/getOrders")
    public List<OrderShowVO> getOrders(@RequestParam(required = false)
                                                   String status){
        List<OrderShowVO> orderShow=new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userName","lijialong");
        queryWrapper.eq("is_delete","0");
        queryWrapper.select().orderByDesc("update_time");
        if(!status.equals("4")){
            queryWrapper.eq("order_status",status);
        }
        orderList = orderService.list(queryWrapper);
        for (TbOrder order :orderList
             ) {
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("order_id",order.getId());
            itemList= orderItemService.list(queryWrapper1);
            OrderShowVO orderShowVO=new OrderShowVO(order,itemList);
            orderShow.add(orderShowVO);
        }
        return orderShow;
    }


}

